package com.iot.inclinometer.util;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.iot.inclinometer.pojo.SendDataDTO;
import com.xuhao.didi.socket.client.sdk.OkSocket;
import com.xuhao.didi.socket.client.sdk.client.OkSocketOptions;
import com.xuhao.didi.socket.client.sdk.client.connection.IConnectionManager;


public class TCPClient {
    public final static String KEY_MSG = "GB_TCP_RESPONSE";
    public final static int TIMEOUT = 5;

    private String serverIP;
    private int port;
    private Handler mainHandler;


    /**
     * socket通道Manager
     */
   private IConnectionManager socketConnManager;

    public TCPClientReceiver getTcpClientReceiver() {
        return tcpClientReceiver;
    }

    private TCPClientReceiver tcpClientReceiver;

    public TCPClient() {

    }

    public TCPClient(Handler mainHandler) {
        this(null, -1, mainHandler);
    }

    public TCPClient(String serverIP, int port) {
        this(serverIP, port, null);
    }

    public TCPClient(String serverIP, int port, Handler mainHandler) {
        this.serverIP = serverIP;
        this.port = port;
        this.mainHandler = mainHandler;
    }

    public TCPClient open() {
        //调用OkSocket,开启这次连接的通道,拿到通道Manager
        socketConnManager = OkSocket.open(serverIP, port);

        return this;
    }

    public IConnectionManager getSocketConnManager() {
        return socketConnManager;
    }

    /**
     * 注册一个回调接收器
     *
     * @param tcpClientReceiver 回调接收器
     */
    public TCPClient registerReceiver(TCPClientReceiver tcpClientReceiver) {
        if (socketConnManager == null) {
            throw new NullPointerException("socketConnManager is null.");
        }

        if(this.tcpClientReceiver!=null){
            socketConnManager.unRegisterReceiver(tcpClientReceiver);
        }
        this.tcpClientReceiver = tcpClientReceiver;
        //注册Socket行为监听器,SocketActionAdapter是回调的Simple类,其他回调方法请参阅类文档
        socketConnManager.registerReceiver(tcpClientReceiver);

        return this;
    }

    public TCPClient unRegisterReceiver(TCPClientReceiver tcpClientReceiver) {
        if (socketConnManager == null) {
            throw new NullPointerException("socketConnManager is null.");
        }

        if (this.socketConnManager != null) {
            socketConnManager.unRegisterReceiver(tcpClientReceiver);
        }

        return this;
    }


    public TCPClient connect() {
        return this.connect(null);
    }

    public TCPClient connect(OkSocketOptions socketOptions) {
        if (socketConnManager == null) {
            throw new NullPointerException("socketConnManager is null.");
        }

        if (socketOptions == null) {
            OkSocketOptions.Builder builder = new OkSocketOptions.Builder();
            //修改参配设置(其他参配请参阅类文档)
            builder.setConnectTimeoutSecond(TCPClient.TIMEOUT);
            builder.setReaderProtocol(new TCPClientReceiver.TCPClientReaderProtocol());
            //建造一个新的参配对象并且付给通道
            socketOptions = builder.build();
        }

        // 设置参数
        socketConnManager.option(socketOptions);

        if(socketConnManager.isConnect()){
            socketConnManager.disconnect();
        }

        //调用通道进行连接
        socketConnManager.connect();

        return this;
    }

    public TCPClient send(SendDataDTO sendDataDTO) {
        if (socketConnManager == null) {
            throw new NullPointerException("socketConnManager is null.");
        }

        socketConnManager.send(sendDataDTO);

        return this;
    }

    public TCPClient disconnect() {
        if (socketConnManager == null) {
            throw new NullPointerException("socketConnManager is null.");
        }

//        if (socketConnManager.isConnect()) {
            socketConnManager.disconnect();
//        }

        return this;
    }


    public TCPClient sendMessage(String msg) {
        return this.sendMessageToUI(0x9527,msg);
    }

    public TCPClient sendConnFailedMessage(String msg) {
        return this.sendMessageToUI(0x9526,msg);
    }

    public TCPClient sendConnOkMessage(String msg) {
        return this.sendMessageToUI(0x9525,msg);
    }

    public TCPClient sendClosedMessage(String msg) {
        return this.sendMessageToUI(0x9528,msg);
    }

    public TCPClient sendMessageToUI(int what,String msg) {
        if (this.mainHandler == null) {
            return this;
        }

        // 步骤4:通知主线程,将接收的消息显示到界面
        Bundle data = new Bundle();
        data.putString(KEY_MSG, msg);

        Message msgObj = Message.obtain();
        msgObj.what = what;

        msgObj.setData(data);

        this.mainHandler.sendMessage(msgObj);

        return this;
    }
}
